/* Unit test for AES encryption */

#include "../lib/jAes.h"
#include "../lib/jRsa.h"
#include "../lib/exceptions.h"

#include <string>
using std::string;

#include <iostream>
using std::cout;
using std::endl;

#include <utility>
using std::pair;

int main() 
{
    //Generate incorrect keysize
    cout << "Generating incorrect keysize..." << endl;
    try
    {
       string wrong = jAes::generateKey( 10 );
    }
    catch( BaseException* e )
    {
        cout << e->what() << endl;
    }

    //Generate correct keysize
    cout << "Generating correct keysize..." << endl;
    string key;
    try
    {
        key = jAes::generateKey( 32 );
    }
    catch( BaseException* e )
    {
        cout << e->what() << endl;
    }
    
    cout << "String representation of key: Size," << key.size() << endl;
    cout << key << endl << endl;

    cout << "-----------------Encryption/Decryption Test-------------------" << endl;
    string msg1( "Hello World!" );
    cout << "Text to encrypt: " << msg1 << endl;
    cout << "Text Size: " << msg1.size() << endl << endl;

    string cipher = jAes::encrypt( key, msg1 );

    cout << "Cipher Text: " << cipher << endl;
    cout << "Cipher Size: " << cipher.size() << endl << endl;

    string plaintext = jAes::decrypt( key, cipher );

    cout << "Decrypted Text: " << plaintext << endl;

    cout << "-----------------Encryption/Decryption Test2-------------------" << endl;
    string msg2( 1024, 'a' );
    cout << "Text to encrypt: " << msg2 << endl;
    cout << "Text Size: " << msg2.size() << endl << endl;

    string cipher2 = jAes::encrypt( key, msg2 );

    cout << "Cipher Text: " << cipher2 << endl;
    cout << "Cipher Size: " << cipher2.size() << endl << endl;

    string plaintext2 = jAes::decrypt( key, cipher2 );

    cout << "Decrypted Text: " << plaintext2 << endl;


    cout << "----------------Session Key Encryption-----------------------" << endl;
    pair<jRsa::publicKey, jRsa::privateKey> rsaKeys= jRsa::generateKeys( 1024 );
    string sessionCipher = jRsa::encrypt( key, rsaKeys.first );
    cout << sessionCipher << endl;

    cout << sessionCipher.size() << endl;

    return 0;
}
